﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PHMEE.Entities.MqttDt.CollectPara;
using PHMEE.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace PHMEE
{
    /// <summary>
    /// 振动通道的仓库
    /// </summary>
    public class VibChannelParaRepository : EfCoreRepository<PHMEEDbContext, VibChannelPara, Guid>, IVibChannelParaRepository
    {
        private ILogger<VibChannelParaRepository> _logger;
        public VibChannelParaRepository(IDbContextProvider<PHMEEDbContext> dbContextProvider, ILogger<VibChannelParaRepository> logger) : base(dbContextProvider)
        {
            _logger = logger;
        }
        /// <summary>
        /// 根据CollectParameter查出--过滤没有选择测点的
        /// </summary>
        /// <param name="CollectParameterID"></param>
        /// <returns></returns>
        public async Task<List<VibChannelPara>> GetByVibChannelWithMqttAsync(Guid SysParaID)
        {
            var dbContext = await GetDbContextAsync();
            return await dbContext.VibChannels
                .Include(vc => vc.VibParaList)
                .OrderBy(vc => vc.ChNo)
                .Where(vc => vc.SysParaID == SysParaID && !string.IsNullOrEmpty(Convert.ToString(vc.PointId)))
                .ToListAsync();
        }
        /// <summary>
        /// 根据CollectParameter查出
        /// </summary>
        /// <param name="CollectParameterID"></param>
        /// <returns></returns>
        public async Task<List<VibChannelPara>> GetAllByVibChannelWithMqttAsync(Guid SysParaID)
        {
            var dbContext = await GetDbContextAsync();
            return await dbContext.VibChannels
                .Include(vc => vc.VibParaList)
                //.Include(vc => vc.LimitParaList)
                .OrderBy(vc => vc.ChNo)
                .Where(vc => vc.SysParaID == SysParaID)
                .ToListAsync();
        }
        /// <summary>
        /// 查询单个通道对应的测点
        /// </summary>
        /// <param name="SysParaID"></param>
        /// <returns></returns>
        public async Task<(Guid PointId, Guid Id)> GetByCollectPoint(Guid SysParaID, int chno)
        {
            var dbContext = await GetDbContextAsync();
            var vib = await dbContext.VibChannels
                .Include(vc => vc.VibParaList)
                .OrderBy(vc => vc.ChNo)
                .Where(vc => vc.SysParaID == SysParaID && vc.ChNo == chno)
                .FirstAsync();
            if (vib == null)
            {
                _logger.LogError("未找到匹配的记录");
            }

            return ((Guid)vib.PointId, vib.Id);
        }

        /// <summary>
        /// 根据通道获取表头
        /// </summary>
        /// <param name="VibChannelParaId"></param>
        /// <returns></returns>
        public async Task<SysPara> GetByVibChannelPara(Guid PointId)
        {
            var dbContext = await GetDbContextAsync();
            return (await dbContext.VibChannels.Include(item => item.SysPara).FirstOrDefaultAsync(m => m.PointId == PointId))?.SysPara;
        }

        /// <summary>
        /// 根据 SysParaID 和 ChNo 获取 VibChannelPara
        /// </summary>
        /// <param name="sysParaId">系统参数 ID</param>
        /// <param name="chNo">通道号</param>
        /// <returns></returns>
        public async Task<VibChannelPara> GetVibChannelBySysParaIdAndChNoAsync(Guid sysParaId, int chNo)
        {
            var dbContext = await GetDbContextAsync();
            return await dbContext.VibChannels
                .FirstOrDefaultAsync(vc => vc.SysParaID == sysParaId && vc.ChNo == chNo);
        }
    }
}
